using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using Gencode.Gengui;

namespace Personalplaner
{
    class GPersonGridGenerator
    {
        internal enum GRIDTYPE { Divers, Jahresplan, Ferien }

        static internal GUGrid CreateGrid(GUGrid grid, DataTable dt, GRIDTYPE gridType, List<IPersonRow> row)
        {
            //max grid size
            int size = 0;
            int maxRowIndex = 0;
            for (int i = 0; i < row.Count; i++)
            {
                if (size < row[i].Column.Count)
                {
                    size = row[i].Column.Count;
                    maxRowIndex = i;
                }
            }

            grid.DataSource = createDataTable(maxRowIndex, dt, gridType, row);

            //Set Data
            for (int r = 0; r < row.Count; r++)
            {
                for (int c = 0; c < row[r].Column.Count; c++)
                {
                    grid[c, r].Value = row[r].Column[c].Wert;
                }
            }

            foreach (DataGridViewColumn column in grid.Columns)
            {
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                column.SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            return grid;
        }

        static private DataTable createDataTable(int maxRowIndex, DataTable dt, GRIDTYPE gridType, List<IPersonRow> row)
        {
            //http://dzaebel.net/DataGridViewBeispiel.htm

            if (row.Count > 0)
            {
                int columnIndex = 0;
                while (columnIndex < row[maxRowIndex].Column.Count)
                {
                    dt.Columns.Add(row[maxRowIndex].Column[columnIndex].Caption);
                    columnIndex++;
                }

                int rowIndex = 0;
                while (rowIndex < row.Count)
                {
                    DataRow dr = dt.NewRow();
                    dt.Rows.Add(dr);
                    rowIndex++;
                }
            }

            return dt;
        }
    }
}
